<template>
  <section class="feedback-list">
    <!-- 搜索栏 -->
    <el-form :inline="true" :model="searchForm" class="search-form">
      <el-form-item label="用户名">
        <el-input v-model="searchForm.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="creator" label="创建人">
        <template slot-scope="scope">
          {{ scope.row.creator || '暂无' }}
        </template>
      </el-table-column>
      <el-table-column prop="content" label="意见内容">
        <template slot-scope="scope">
          {{ scope.row.content || '暂无内容' }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间">
        <template slot-scope="scope">
          {{ scope.row.createTime || '暂无' }}
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="更新时间">
        <template slot-scope="scope">
          {{ scope.row.updateTime || '暂无' }}
        </template>
      </el-table-column>
      <el-table-column prop="updator" label="更新人">
        <template slot-scope="scope">
          {{ scope.row.updator || '暂无' }}
        </template>
      </el-table-column>
      <el-table-column prop="jobSeekerId" label="求职者ID" width="100">
        <template slot-scope="scope">
          {{ scope.row.jobSeekerId || '暂无' }}
        </template>
      </el-table-column>
      <el-table-column prop="isDelete" label="状态" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isDelete === 1 ? 'success' : scope.row.isDelete === 0 ? 'danger' : 'info'">
            {{ scope.row.isDelete === 1 ? '正常' : scope.row.isDelete === 0 ? '已删除' : '未知' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button 
            size="mini"
            type="primary"
            @click="handleView(scope.row.id)">
            查看
          </el-button>
          <el-button 
            size="mini" 
            type="danger" 
            @click="handleDelete(scope.row.id)"
            v-if="scope.row.isDelete === 1">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageCurrent"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 处理对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
      <el-form :model="form" ref="form" label-width="100px">
        <el-form-item label="处理结果" prop="result">
          <el-input 
            type="textarea" 
            v-model="form.result" 
            :rows="4"
            placeholder="请输入处理结果">
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 查看详情对话框 -->
    <el-dialog title="查看详情" :visible.sync="viewDialogVisible" width="500px">
      <el-form label-width="100px">
        <el-form-item label="内容">
          <span>{{ viewForm.content || '暂无内容' }}</span>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="viewDialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </section>
</template>

<script>
export default {
  name: 'FeedbackList',
  data() {
    return {
      searchForm: {
        username: ''
      },
      tableData: [],
      pageCurrent: 1,
      pageSize: 10,
      total: 0,
      dialogVisible: false,
      dialogTitle: '处理意见',
      form: {
        id: '',
        result: ''
      },
      viewDialogVisible: false,
      viewForm: {
        content: ''
      }
    }
  },
  methods: {
    // 搜索
    handleSearch() {
      this.$axios.get('/unlike/get', {
        params: {
          username: this.searchForm.username || null
        }
      }).then(res => {
        if (res.code === 200) {
          this.tableData = res.data || [];
          this.total = res.data.length || 0;
        } else {
          this.$message.error(res.msg || '查询失败');
        }
      }).catch(error => {
        console.error('查询出错:', error);
        this.$message.error('查询失败，请稍后重试');
      });
    },
    
    // 重置搜索
    resetSearch() {
      this.searchForm = {
        username: ''
      }
      this.handleSearch()
    },
    
    // 查看详情
    handleView(id) {
      this.$axios.get(`/unlike/getBy/${id}`).then(res => {
        if (res.code === 200) {
          this.viewForm = {
            content: res.msg
          };
          this.viewDialogVisible = true;
        } else {
          this.$message.error(res.msg || '获取详情失败');
        }
      }).catch(error => {
        console.error('获取详情出错:', error);
        this.$message.error('获取详情失败，请稍后重试');
      });
    },
    
    // 提交处理结果
    submitForm() {
      this.$axios.get('/unlike/process', { 
        params: this.form 
      }).then(res => {
        if (res.code === 200) {
          this.$message.success('处理成功')
          this.dialogVisible = false
          this.handleSearch()
        }
      })
    },
    
    // 分页处理
    handleSizeChange(val) {
      this.pageSize = val
      this.handleSearch()
    },
    
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.handleSearch()
    },

    // 删除
    handleDelete(id) {
      this.$confirm('确认删除该意见吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.delete(`/unlike/delete/${id}`).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功');
            this.handleSearch();
          } else {
            this.$message.error(res.msg || '删除失败');
          }
        });
      }).catch(() => {});
    }
  },
  mounted() {
    this.handleSearch()
  }
}
</script>

<style lang="less" scoped>
.feedback-list {
  padding: 20px;
  
  .search-form {
    margin-bottom: 20px;
  }
  
  .el-pagination {
    margin-top: 20px;
    text-align: right;
  }
}
</style> 